define([ 'backbone',
         'components/pop/base',
         'text!components/pop/templates/link.html',
         'core/utils' ],
function( Backbone, Base, template, utils ) {
    return Base.extend({
        template: _.template(template),
        className: 'popover popover-link-wrap bottom',
        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback',
            'keydown .js-link-placeholder': function(e) {
                if (e.keyCode === utils.keyCode['ENTER']) {
                    this.triggerCallback();
                }
            }
        },
        initialize: function(options) {
            this.content = options.content || '链接地址：http://example.com';
            this.callback = options.callback;
            this.target = options.target;
            this.trigger = options.trigger;
            if (!options.notAutoHide) {
                this.autoHide();
            }
        },
        render: function(options) {
            this.$el.html(this.template({
                content: this.content
            }));
            return this;
        },
        positioning: function() {
            this.$el.show().position({
                of: this.target,
                my: 'center top+5',
                at: 'center bottom',
                collision: 'none'
            });

            this.$('.js-link-placeholder').focus();
        },
        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                content = obj.content || '链接地址：http://example.com';

            this.$('.js-link-placeholder').attr('placeholder', content);

            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.positioning();
            this.clearInput();
            this.show();
        },
        triggerCallback: function() {
            var data = utils.urlCheck(this.$('.js-link-placeholder').val());

            this.callback.call(this, data);
            this.hide();
        },
        clearInput: function() {
            this.$('.js-link-placeholder').val('');
        }
    });
});
